package com.ciquan.server.job;

import com.ciquan.server.bean.PushMessage;
import com.ciquan.server.bean.UserInfo;
import com.ciquan.server.bean.WorksInfo;
import com.ciquan.server.service.PushMessageService;
import com.ciquan.server.service.WorksInfoService;
import com.ciquan.server.util.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by card on 15/9/17.
 */
public class PushJob {

    @Resource
    private WorksInfoService worksInfoService;

    @Resource
    private PushMessageService pushMessageService;

    private Logger logger = Logger.getLogger(this.getClass().getName());

    private ExecutorService pool;

    public PushJob() {
        pool = Executors.newCachedThreadPool();
    }

    public void execute() {
        logger.info("---------------------开始执行PushJob---------------------");
        String starTime = DateUtils.miniute(-2);
        String endTime = DateUtils.miniute(-1);
        try {
            List<WorksInfo> worksInfos = worksInfoService.selectByTime(starTime, endTime);

            for(WorksInfo worksInfo : worksInfos) {
                sendToAttention(worksInfo);
            }

            logger.info("---------------------执行PushJob结束 执行作品数量："+worksInfos.size()+"---------------------");
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private void sendToAttention(WorksInfo worksInfo) {

        PushMessage pushMessage = new PushMessage();
        pushMessage.setTitle("瓷圈提醒");
        pushMessage.setContent("你关注的【" + worksInfo.getArtistName() + "】有了新藏品，点击查看");
        pushMessage.setType("4" + worksInfo.getArtistid());

        List<UserInfo> userInfos = worksInfoService.attentionArtist(worksInfo.getArtistid());
        for (UserInfo userInfo : userInfos) {
            pool.execute(new PushTask(userInfo, pushMessage));
        }

        pushMessage = new PushMessage();
        pushMessage.setTitle("瓷圈提醒");
        pushMessage.setContent("你关注的【" + worksInfo.getArtistName() + "】有了新藏品，点击查看");
        pushMessage.setType("5" + worksInfo.getUserid());
        pushMessage.setContent("你关注的【" + worksInfo.getUsername() + "】有了新藏品，点击查看");
        userInfos = worksInfoService.attentionUser(worksInfo.getUserid());
        for (UserInfo userInfo : userInfos) {
            pool.execute(new PushTask(userInfo, pushMessage));
        }
    }

    private class PushTask implements Runnable {

        private PushMessage pushMessage;

        private UserInfo userInfo;

        public PushTask(UserInfo userInfo, PushMessage pushMessage) {
            this.userInfo = userInfo;
            this.pushMessage = pushMessage;
        }

        @Override
        public void run() {
            if(StringUtils.isNotEmpty(userInfo.getToken())) {
                logger.info("开始推送：" + userInfo.getMobile() + userInfo.getToken() + userInfo.getDeviceType());
                pushMessageService.sendMessage(userInfo, pushMessage);
            }
        }
    }
}
